package com.mvp.lt.airlineview.loopview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.RxObservableTransformer;
import com.mvp.lt.airlineview.base.BaseActivity;
import com.android.view.weigan.loopview.LoopView;
import com.android.view.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

/**
 * @author LiuTao
 * @description:
 * @date : 2021/3/10 10:42
 */
public class LoopViewActivity extends BaseActivity {
    private Disposable recordTimeDisposable;
    private LoopView mLoopView;
    private List<String> stringArrayList = new ArrayList<>();
    private TextView mTvShow;

    public static void show(Context context) {
        Intent intent = new Intent(context, LoopViewActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (recordTimeDisposable != null) {
            recordTimeDisposable.dispose();
        }
    }

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lopper_view);

        for (int i = 0; i <= 90; i++) {
            stringArrayList.add("" + i);
        }
        findViewById(R.id.btn_0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLoopView.setCurrentPosition(0);
            }
        });
        findViewById(R.id.btn_10).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLoopView.setCurrentPosition(10);
            }
        });
        findViewById(R.id.btn_50).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLoopView.setCurrentPosition(50);
            }
        });
        findViewById(R.id.btn_90).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLoopView.setCurrentPosition(90);
            }
        });
        mTvShow = findViewById(R.id.tv_show);
        mLoopView = findViewById(R.id.loopView);
        mLoopView.setItems(stringArrayList);
        mLoopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mTvShow.setText("index:" + index + ",name:" + stringArrayList.get(index));
            }
        });
        mLoopView.setCanTouch(true);
        Observable<String> listObservable = Observable.fromIterable(stringArrayList);
        Observable<Long> timerObservable = Observable.interval(1000, TimeUnit.MILLISECONDS);
        //使用zip操作符合并两个Observable
        Observable.zip(listObservable, timerObservable, new BiFunction<String, Long, String>() {
            @Override
            public String apply(String s, Long aLong) throws Exception {
                return s;
            }
        }).compose(RxObservableTransformer.transformer())
                .doOnNext(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        //此处接收
                        mTvShow.setText("name:" + s);
                        mLoopView.setCurrentPosition(stringArrayList.indexOf(s));
                    }
                }).subscribe();

    }


}
